/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tarena.lbs.attach.rpc;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.pojo.dto.AttachDTO;
import com.tarena.lbs.attach.pojo.dto.AttachQrDTO;
import com.tarena.lbs.attach.pojo.param.AttachQRParam;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import com.tarena.lbs.attach.service.AttachService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

@DubboService
@Slf4j
public  class AttachRpcService implements AttachApi {
    @Autowired
    private AttachService attachService;

    /**
     * 获取附件信息
     */
    @Override
    public List<AttachDTO> getAttachInfo(AttachQuery attachQuery) {
        if (attachQuery == null) {
            return Collections.emptyList();
        }
        try {
            List<AttachDTO> list = attachService.getAttachInfo(attachQuery);
            return CollectionUtils.isEmpty(list) ? Collections.emptyList() : list;
        } catch (Exception e) {
            log.error("获取附件信息异常", e);
            return Collections.emptyList();
        }
    }

    /**
     * 批量绑定图片业务
     */
    @Override
    public boolean batchUpdateBusiness(List<PicUpdateParam> picParams) {
        if (CollectionUtils.isEmpty(picParams)) {
            return false;
        }
        try {
            return attachService.batchUpdateBusiness(picParams);
        } catch (Exception e) {
            log.error("批量绑定图片业务异常", e);
            return false;
        }
    }

    /**
     * 生成二维码
     */
    @Override
    public AttachQrDTO generateQrCode(AttachQRParam attachQRParam) {
        if (attachQRParam == null) {
            return null;
        }
        try {
            return attachService.generateQrCode(attachQRParam);
        } catch (Exception e) {
            log.error("生成二维码异常", e);
            return null;
        }
    }
}
